package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Slf4j
@RequestMapping("v1/file/")
@RestController
public class FilterUploadController {
    @PostMapping("upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        log.debug("上传的文件名：{}",filename);
        String suffix = filename.substring(filename.lastIndexOf("."));
        log.debug("文件后缀名：{}",suffix);
        filename = UUID.randomUUID()+suffix;
        log.debug(filename);
        String dirPaht ="d:/files";
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String dataPath = sdf.format(new Date());
        File file1 =new File(dirPaht+ dataPath);
        if (!file1.exists()){
            file1.mkdirs();
        }
        log.debug("保存文件的目录：{}",file1.getAbsolutePath());
        String filePath =dirPaht+dataPath+filename;
        log.debug("保存文件的路径：{}",filePath);
        file.transferTo(new File(filePath));
        return JsonResult.ok(dataPath+filename);
    }
}
